package com.lanou.dllo.findfood.adpter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.UserTop_by_city;
import com.lanou.dllo.findfood.volley.VolleySington;


import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class HotUsertopAdpter extends RecyclerView.Adapter<HotUsertopAdpter.topAdpter> {
    private List<UserTop_by_city> user_results;
    private ImageLoader imageLoader;
    private RequestQueue queue;
    private ImageLoader.ImageCache imageCache;
    private LruCache<String, Bitmap> lruCache;//第一个参数是网址  第二个参数是图片信息

    //初始化
    public HotUsertopAdpter(Context context) {
        queue = Volley.newRequestQueue(context);
        lruCache = new LruCache<String, Bitmap>(30);//缓存大小
        imageCache = new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {

                return lruCache.get(url);//得到一个缓存的网络地址
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                lruCache.put(url, bitmap);

            }
        };
        imageLoader = new ImageLoader(queue, imageCache);


    }

    public void getBitmapCatch(String url, ImageView imageView) {

        //三个参数  没有请求到内容时显示的图片
        ImageLoader.ImageListener listener = imageLoader.getImageListener(imageView
                , R.mipmap.ic_launcher, R.mipmap.ic_launcher);
        imageLoader.get(url, listener);
    }


    public void addData(List<UserTop_by_city> cities) {
        user_results = cities;
        notifyDataSetChanged();
    }

    class topAdpter extends RecyclerView.ViewHolder {
        private NetworkImageView userIv;
        private TextView user_name,itemNumber,
                topreputation,toppicture_count,topsex,toplevel_title;
        private  ImageView heardImg;

        public topAdpter(View itemView) {
            super(itemView);
            userIv = (NetworkImageView) itemView.findViewById(R.id.userimage);
            user_name = (TextView) itemView.findViewById(R.id.topusename);
            itemNumber = (TextView) itemView.findViewById(R.id.itemNumber);
            topreputation = (TextView) itemView.findViewById(R.id.topreputation);
            toppicture_count = (TextView) itemView.findViewById(R.id.toppicture_count);
            topsex = (TextView) itemView.findViewById(R.id.topsex);
            toplevel_title = (TextView) itemView.findViewById(R.id.toplevel_title);
            heardImg = (ImageView) itemView.findViewById(R.id.heardImg);


        }
    }


    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }
    @Override
    public topAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.reclcey_user_top_item, null);

        return new topAdpter(view);
    }

    @Override
    public void onBindViewHolder(topAdpter holder, int position) {
        if (user_results != null && user_results.size() > 0) {
            UserTop_by_city city = user_results.get(position);
            holder.user_name.setText(city.getName());
            String imgUrl = city.getUser_image();
            if (imgUrl != null && imgUrl.length() > 0) {
                getBitmap(imgUrl, holder.userIv);
            }

          //  holder.itemNumber.setText(""+user_results.size());
            holder.topreputation.setText(city.getReputation()+"");
            holder.toppicture_count.setText(city.getPicture_count()+"");

                int se = city.getSex();
                if (se == 0) {
                    holder.topsex.setText("女");
                } else if (se == 1) {
                    holder.topsex.setText("男");
                } else {
                    holder.topsex.setText("null");
                }
            holder.toplevel_title.setText(city.getLevel_title());





                if (position !=0) {
                    Bitmap bitmap = BitmapFactory.decodeResource(Resources.getSystem(), R.mipmap.gold_android);
                    holder.heardImg.setImageBitmap(bitmap);
                }

            holder.itemNumber.setText(position + 1 + "");
            }




        }




    @Override
    public int getItemCount() {
        return user_results != null && user_results.size() > 0 ? user_results.size() : 0;
    }


}
